#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 15
#define WIN_COUNT 5

char board[N][N];

void init_board() 
{int i,j; 
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) 
		{
            board[i][j] = '.';
        }
    }
}

void display_board()
 {int i,j;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            printf("%c ", board[i][j]);
        }
        printf("\n");
    }
}

int is_valid_move(int x, int y) {
    return x >= 0 && x < N && y >= 0 && y < N && board[x][y] == '.';
}

int is_winner(int x, int y, char player)
 {int i,j;
    int directions[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    for (i = 0; i < 4; i++) {
        int count = 1;
        int dx = directions[i][0];
        int dy = directions[i][1];

        int nx = x + dx;
        int ny = y + dy;
        while (nx >= 0 && nx < N && ny >= 0 && ny < N && board[nx][ny] == player) {
            count++;
            nx += dx;
            ny += dy;
        }

        nx = x - dx;
        ny = y - dy;
        while (nx >= 0 && nx < N && ny >= 0 && ny < N && board[nx][ny] == player) {
            count++;
            nx -= dx;
            ny -= dy;
        }

        if (count >= WIN_COUNT) {
            return 1;
        }
    }
    return 0;
}

int main() {
    srand(time(0));
    int current_player = rand() % 2;
    char players[2] = {'O', 'X'};

    init_board();
    display_board();

    while (1) {
        int x, y;
        printf("Player %c's turn. Enter the coordinates (x, y): ", players[current_player]);
        scanf("%d %d", &x, &y);

        if (is_valid_move(x, y)) {
            char player = players[current_player];
            board[x][y] = player;
            display_board();

            if (is_winner(x, y, player)) {
                printf("Player %c wins!\n", player);
                break;
            }

            current_player = 1 - current_player;
        } else {
            printf("Invalid move. Try again.\n");
        }
    }

    return 0;
}

